package com.zys.http.extension.search.everywhere;

import com.intellij.ide.projectView.PresentationData;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProvider;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.NavigatablePsiElement;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.zys.http.entity.tree.MethodNodeData;
import com.zys.http.ui.icon.HttpIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.KtClass;
import org.jetbrains.kotlin.psi.KtNamedFunction;

import javax.swing.*;
import java.util.Optional;

/**
 * @author zhou ys
 * @since 2023-12-21
 */
public class GotoApiPresentationProvider implements ItemPresentationProvider<GotoApiItem> {
    @Override
    public ItemPresentation getPresentation(@NotNull GotoApiItem gotoApiItem) {
        MethodNodeData methodNodeData = gotoApiItem.methodNodeData();
        NavigatablePsiElement psiElement = methodNodeData.getPsiElement();
        Icon icon = HttpIcons.HttpMethod.getHttpMethodIcon(methodNodeData.getHttpMethod());
        if (psiElement instanceof PsiMethod psiMethod) {
            PsiClass psiClass = ApplicationManager.getApplication().runReadAction((Computable<PsiClass>) () -> (PsiClass) psiMethod.getParent());
            String location = Optional.ofNullable(psiClass).map(NavigationItem::getName).orElse("") + "#" + psiMethod.getName();
            return new PresentationData(gotoApiItem.getName(), location, icon, null);
        } else if (psiElement instanceof KtNamedFunction function) {
            String location = ApplicationManager.getApplication().runReadAction((Computable<String>) () ->
                    Optional.ofNullable(function.getParent().getParent()).filter(KtClass.class::isInstance)
                            .map(KtClass.class::cast).map(NavigationItem::getName).orElse("") + "#" + function.getName()
            );
            return new PresentationData(gotoApiItem.getName(), location, icon, null);
        }
        return null;
    }
}
